﻿using System.Threading.Tasks;
using Controller;
using NN = NNCore;

namespace GACore.ReversiGA
{
	public class NNTournament : Tournament
	{
		public NNTournament(NN.Controller[] nnControllers)
			: base(nnControllers)
		{
		}

		public override void PlayTournament(Vector[] vectors)
		{
			base.PlayTournament(vectors);

			Parallel.For(0, vectors.Length,
				i =>
				{
					Vector<double> v = (Vector<double>)vectors[i];
					vectors[i].Fitness.Reset();
					nnControllers[i].SetNetAsVector(v.Elements);
					PlayGame(PlayerTypes.KI, PlayerTypes.NN, -1, i);
					PlayGame(PlayerTypes.NN, PlayerTypes.KI, i, -1);
				});

			Parallel.For(0, vectors.Length - 1,
				i =>
				{
					for (int j = i + 1; j < vectors.Length; j++)
					{
						PlayGame(i, j);
						PlayGame(j, i);
					}
				});
		}

		private void PlayGame(int id1, int id2)
		{
			PlayGame(PlayerTypes.NN, PlayerTypes.NN, id1, id2);
		}
	}
}
